package com.ocom.webapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.webapi.feign.AccessClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class PersonnelSumController {

    @Autowired
    private AccessClientService accessClientService;


    @PostMapping(WebApiServiceUrl.URL_StudentPageSumShow)
    public Result studentPageSumShow(@RequestBody JSONObject json) {

        if (json.getLong("comId")==null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        Result getStudentSumBySchool=accessClientService.getStudentSumBySchool(json.getLong("comId"));
        if (getStudentSumBySchool.getCode()==200){
            List<JSONObject> list=JSON.parseArray(JSON.toJSONString(getStudentSumBySchool.getData()),JSONObject.class);

            for (JSONObject jsonObject : list) {
                if (jsonObject.getLong("regionId")==0){
                    jsonObject.put("regionName","");
                }else {
                    JSONObject regionJsonObject=new JSONObject();
                    regionJsonObject.put("comId",json.getLong("comId"));
                    regionJsonObject.put("regionId",jsonObject.getLong("regionId"));
                    Result getRegionInfo=accessClientService.getRegionInfo(regionJsonObject);
                    if (getRegionInfo.getCode()==200){
                        JSONObject getRegionInfoJson=JSON.parseObject(JSON.toJSONString(getRegionInfo.getData()));
                        List<JSONObject> regionList=JSON.parseArray(JSON.toJSONString(getRegionInfoJson.get("list")),JSONObject.class);
                        for (JSONObject object : regionList) {
                            jsonObject.put("regionName",object.getString("regionName"));
                        }
                    }else {
                        jsonObject.put("regionName","");
                    }
                }

            }
            return ResultUtil.success(list);
        }else {
            return getStudentSumBySchool;
        }
    }

}
